package cn.shafish.cachedemo.service.serviceImpl;

import cn.shafish.cachedemo.dto.UserDto;
import cn.shafish.cachedemo.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    @Cacheable(value = "userData", key = "#id", sync = true)
    public UserDto getUser(Integer id) {
        System.out.println("执行了getUser方法，开始查询数据库获取id为：" + id + " 的用户数据");
        return new UserDto().setId(id)
                .setName("hello吖")
                .setOther("da mie da");
    }

    @Override
    @CachePut(value = "userData", key = "#id")
    public UserDto updateUser(Integer id) {
        System.out.println("执行了updateUser方法，更改了name值，并进行了缓存");
        return new UserDto().setId(id)
                .setName("ki mo ji")
                .setOther("da mie da");
    }

    @Override
    @CacheEvict(value = "userData", key = "#id")
    public void delete(Integer id) {
        System.out.println("执行了delete方法，再调用 user/hello 看看还有没有值");
    }

}
